module(..., package.seeall)

require "misc"
require "net"
require "myUiWin"
require "sim"
require "deviceConfig"
require "tool"
require "config"
require "aLiIotda"
require "hwIotda"
-- require "YaLiIotda"
-- require "mqttTask"

-- PAGEID：窗口id
PAGEID = 0
local mqtt_ready = false
local aLi_mqtt_ready = false
local showTimeSplit = false -- 通过轮替显示logo
local backColor = 0x00

local time_count = 0
local scan_count = 1
local page_turn = 0
local show_id = 0
local r_color = 0x06C0
local show_first_key = 1 -- 当前显示菜单页面第一行 对应table里面的一级菜单索引key值
local data_pub = false

local function getPowerState(state)
    power_state = state
end

-- 画电源状态
local function powerDraw()
    if power_state == 1 then
        r_color = 0xD8E0
    else
        r_color = 0x06C0
    end
end

local dev_data = { "---", "---", "---", "---", "---", "---", "---", "---" }
local dev_addr = {}

local function timeStick()
    -- 设备多于5台，自动进行翻页
    local l_len = #dev_addr

    if l_len > 5 then
        page_turn = page_turn + 1
    end

    if page_turn >= 1 then
        page_turn = 0
        if show_first_key == 1 then
            show_first_key = 6
        elseif show_first_key == 6 then

            show_first_key = 1
        end
    end
end
local function rssiDraw()
    local x, y = 160 - 20, 3
    local rssi = net.getRssi()
    local color = 0xFFFF

    if sim.getStatus() ~= true then
        disp.putimage("/lua/noSim.png", 140, 0)
        return
    end

    if net.getNetMode() == 0 then
        color = 0xD8E0
    end
    -- 无信号时，画"- - -"
    if rssi == 0 then
        disp.drawrect(x + 1, y + 9, x + 4, y + 11, color)
        disp.drawrect(x + 6, y + 9, x + 9, y + 11, color)
        disp.drawrect(x + 11, y + 9, x + 15, y + 11, color)
        -- 画1格信号
    elseif rssi > 0 and rssi <= 7 then
        disp.drawrect(x + 2, y + 12, x + 4, y + 16, color)
        -- 画2格信号
    elseif rssi > 7 and rssi <= 15 then
        disp.drawrect(x + 2, y + 12, x + 4, y + 16, color)
        disp.drawrect(x + 6, y + 9, x + 8, y + 16, color)
        -- 画3格信号
    elseif rssi > 15 and rssi <= 23 then
        disp.drawrect(x + 2, y + 12, x + 4, y + 16, color)
        disp.drawrect(x + 6, y + 9, x + 8, y + 16, color)
        disp.drawrect(x + 10, y + 5, x + 12, y + 16, color)
        -- 画4格信号
    elseif rssi > 23 then
        disp.drawrect(x + 2, y + 12, x + 4, y + 16, color)
        disp.drawrect(x + 6, y + 9, x + 8, y + 16, color)
        disp.drawrect(x + 10, y + 5, x + 12, y + 16, color)
        disp.drawrect(x + 14, y + 1, x + 16, y + 16, color)
    end
end

-- 画实时时间
local function timeDraw()
    local tm = misc.getClock()
    if time_count > 9 then
        showTimeSplit = not showTimeSplit
        time_count = 0
    end
    if showTimeSplit == false then
        disp.puttext(string.format("%02d-%02d %02d %02d", tm.month, tm.day,
                tm.hour, tm.min), 5, 5)
    else
        disp.puttext(string.format("%02d-%02d %02d:%02d", tm.month, tm.day,
                tm.hour, tm.min), 5, 5)
    end
    -- disp.puttext(string.format("%0.1f", 1024 - collectgarbage("count")),55,2)
end

-- 画MQTT服务器链接状态
local function connectDraw()
    if aLiIotda.isReady() == true and socket.isReady() == true and
            hwIotda.isReady() == true then
        if data_pub then
            disp.putimage("/lua/online2.png", 115, 4)
            data_pub = false
        else
            disp.putimage("/lua/online.png", 115, 4)
        end

    else
        disp.putimage("/lua/offline.png", 115, 4)
    end

end

local function setData(value)
    log.info("GET DATA", value)
    local res = tool.Split(value, "|")
    for i, data in ipairs(dev_addr) do
        if res[1] == data then
            dev_data[i] = res[2]
        end
    end
end

-- 显示设备列表
local function devShow()
    if #dev_addr == 0 then
        disp.puttext(common.utf8ToGb2312("当前网关"),
                ui_handle.getXPos(common.utf8ToGb2312("当前网关")), 50)
        disp.puttext(common.utf8ToGb2312("未设定设备类型"),
                ui_handle.getXPos(
                        common.utf8ToGb2312("未设定设备类型")), 75)
    else
        for i = 1, 5 do
            local index = show_first_key + i - 1
            if (dev_addr[index] ~= nil and dev_data[index] ~= nil) then
                disp.puttext(dev_addr[index] .. ": " .. dev_data[index], 10,
                        3 + i * 20 + 7)
            end
        end

    end
end

-- 窗口初始化函数
function initPage()
    dev_data = { "---", "---", "---", "---", "---", "---", "---", "---" }
    page_turn = 0
    show_first_key = 1
    local id = 1
    deviceConfig.testDevice(0)

    dev_addr = tool.deepCopy(nvm.get("DEV_SUB_ADDR"))
    log.info("DEV LEN", #dev_addr)
    table.sort(dev_addr)
    disp.setcolor(0xFFFF)
    disp.setbkcolor(backColor)
    disp.clear()
    disp.drawrect(0, 25, 105, 27, 0x017E)

    if r_color == 0xD8E0 and showTimeSplit == false then
        disp.drawrect(110, 25, 160, 27, r_color)
    elseif r_color == 0x06C0 then
        disp.drawrect(110, 25, 160, 27, r_color)
    end

    rssiDraw()
    timeDraw()
    connectDraw()
    devShow()
    powerDraw()

    disp.update()
end

-- 定时刷新函数
function refresh()
    disp.setcolor(0xFFFF)
    disp.setbkcolor(backColor)
    disp.clear()
    disp.drawrect(0, 25, 105, 27, 0x017E)

    if r_color == 0xD8E0 and showTimeSplit == false then
        disp.drawrect(110, 25, 160, 27, r_color)
    elseif r_color == 0x06C0 then
        disp.drawrect(110, 25, 160, 27, r_color)
    end

    rssiDraw()
    timeDraw()
    connectDraw()
    devShow()
    powerDraw()

    time_count = time_count + 1
    disp.update()
end

function onKey(value)
    if value == "enter" then
        myUiWin.openWin(ui_password.PAGEID, "index")
        return
    end
    if value == "down" then
        myUiWin.openWin(ui_qrcode.PAGEID, "选择菜单")
        return
    end
    if value == "return" then
        if show_id == 0 then
            show_id = 1
        elseif show_id == 1 then
            show_id = 0
        end
    end

end

function set_pub()
    data_pub = true
end

sys.subscribe("GET_485_DATA", setData)
sys.subscribe("PUB", set_pub)
sys.subscribe("POWER_STATE", getPowerState)
sys.subscribe("TIME_STICK", timeStick)
